package com.xiangyueEducation.uploaderCloud.controller;
import com.xiangyueEducation.uploaderCloud.context.BaseContext;
import com.xiangyueEducation.uploaderCloud.pojo.dto.PageQuery;
import com.xiangyueEducation.uploaderCloud.pojo.entity.Doctor;
import com.xiangyueEducation.uploaderCloud.pojo.vo.PageVo;
import com.xiangyueEducation.uploaderCloud.service.DoctorService;
import com.xiangyueEducation.uploaderCloud.utils.Result;
import com.xiangyueEducation.uploaderCloud.utils.enums.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @PostMapping("/teacher")
    public Result addDoctor(@RequestBody Doctor doctor){
        doctor.setDoctorName(BaseContext.getCurrentUser().getRealName());
        doctor.setUserId(BaseContext.getCurrentUser().getId());
        doctorService.save(doctor);
        Long id = doctor.getId();
        return Result.ok(id);
    }
    //根据教师姓名进行条件查询，根据rating排序
    @GetMapping("/all")
    public Result queryDoctor(PageQuery pageQuery, String keyword){
        Result result = pageQuery.checkPage();
        if (result!=null){
            return result;
        }
        PageVo pageVo = doctorService.pageQuery(pageQuery,keyword);
        return Result.ok(pageVo);
    }
    //修改教师信息
    @PutMapping("/teacher")
    public Result updateTeacher(@RequestBody Doctor doctor){
        if (doctor.getId()==null)return Result.build(doctor, ResultCodeEnum.MUST_HAVE_ID);
        //只能修改自己的信息
       if(!doctor.getUserId().equals(BaseContext.getCurrentUser().getId())){
          return Result.build(doctor,ResultCodeEnum.ONLY_CAN_UPDATE_YOURSELF);
       }
        //在xml文件中实现动态更新
        doctorService.update(doctor);
        return Result.ok(ResultCodeEnum.SUCCESS);
    }
}
